package com.tcxhb.job.admin.controller;

import com.tcxhb.job.common.constants.enums.ScheduleStatus;
import com.tcxhb.job.core.constants.enums.LogActionEnum;
import com.tcxhb.job.core.constants.enums.OnlineStatus;
import com.tcxhb.job.dao.dataobject.JobInstanceDO;
import com.tcxhb.job.dao.dataobject.JobMachineDO;
import com.tcxhb.job.core.service.job.JobMachineService;
import com.tcxhb.job.core.service.job.JobInstanceService;
import com.tcxhb.job.common.model.MiscResult;
import com.tcxhb.job.common.utils.ParamUtils;
import com.tcxhb.job.core.utils.LogUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * @Description:
 * @Auther: tcxhb
 * @Date: 2023/10/31
 * 对外提供的API
 */
@RestController
@RequestMapping("/api/job")
@Slf4j
public class JobApiController {
    @Autowired
    private JobMachineService machineService;
    @Autowired
    private JobInstanceService instanceService;

    @GetMapping(value = "/beat")
    public MiscResult<Boolean> beat(@RequestParam(value = "host", required = false) String host,
                                    @RequestParam(value = "appName", required = false) String appName,
                                    @RequestParam(value = "session", required = false) String session,
                                    @RequestParam(value = "port", required = false) Integer port
    ) {
        ParamUtils.notEmpty(host, "host is null");
        ParamUtils.notEmpty(appName, "app is null");
        ParamUtils.notNull(port, "port is null");

        JobMachineDO machineDO = new JobMachineDO();
        machineDO.setIp(host);
        machineDO.setPort(port);
        machineDO.setAppName(appName);
        machineDO.setBeatTime(new Date());
        machineDO.setStatus(OnlineStatus.online.getType());
        //返回
        Long id = machineService.create(machineDO);
        return MiscResult.suc(true);
    }

    @GetMapping(value = "/offline")
    public MiscResult<Boolean> offline(@RequestParam(value = "host") String host,
                                       @RequestParam(value = "appName") String appName,
                                       @RequestParam(value = "session", required = false) String session,
                                       @RequestParam(value = "port") Integer port
    ) {
        ParamUtils.notEmpty(host, "host is null");
        ParamUtils.notEmpty(appName, "app is null");
        ParamUtils.notNull(port, "port is null");
        log.info("offline,agent:{},{},{}", host, appName, port);
        JobMachineDO machineDO = new JobMachineDO();
        machineDO.setIp(host);
        machineDO.setPort(port);
        machineDO.setAppName(appName);
        machineDO.setStatus(OnlineStatus.offline.getType());
        //返回
        Boolean result = machineService.updateByHost(machineDO);
        return MiscResult.suc(result);
    }

    @GetMapping(value = "/callback")
    public MiscResult<Boolean> callback(@RequestParam(value = "traceId", required = false) String traceId,
                                        @RequestParam(value = "instanceId", required = false) Long instanceId,
                                        @RequestParam(value = "status", required = false) Integer status,
                                        @RequestParam(value = "msg", required = false) String msg

    ) {
        ParamUtils.notNull(traceId, "traceId is null");
        ParamUtils.notNull(instanceId, "instanceId is null");
        ParamUtils.notNull(status, "status is null");
        JobInstanceDO update = new JobInstanceDO();
        update.setId(instanceId);
        update.setExecuteStatus(status);
        update.setMsg(msg);
        update.setScheduleStatus(ScheduleStatus.SUCC.getType());
        Boolean result = instanceService.updateById(update);
        //为了打日志查询接口
        LogUtils.info(traceId, LogActionEnum.JOBCallback, status + "," + result);
        return MiscResult.suc(result);
    }

    @GetMapping(value = "/server/beat")
    public MiscResult<Boolean> beat() {
        return MiscResult.suc(true);
    }
}

